/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import lombok.Getter;
import lombok.Setter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.google.code.cubeirc.common.ApplicationInfo;
import com.google.code.cubeirc.config.Configuration;
import com.google.code.cubeirc.config.EditorSettings;
import com.google.code.cubeirc.dialogs.AlertBox;

public class EditorPreferences extends Dialog {

	protected Object result;
	protected Shell shell;
	private Button btnBackground;
	private Button btnForeground;
	private Button btnFont;
	private Label lblExample;
	private Label lblTestBox;
	private Button btnClose;
	private Button btnSave;
	private Button btnDefault;

	
	@Getter @Setter
	private String editorName;
	
	public EditorPreferences(Shell parent, int style, String name) {
		super(parent, style);
		setEditorName(name);
		setText("Preferences for editor " + name.replace("EDITOR_", "").replace("_", " "));	
	}

	public Object open() {
		jbInit();
		this.shell.open();
		this.shell.layout();		
		Display display = getParent().getDisplay();
		Monitor primary = display.getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    
	    shell.setLocation(x, y);
	    this.shell.setLayout(null);
	    
	    this.btnBackground = new Button(this.shell, SWT.NONE);
	    this.btnBackground.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent arg0) {
	    		ColorDialog cl = new ColorDialog(shell);
	    		cl.setText("Select color for background");
	    		RGB col =  cl.open();
	    		if (col!= null)
	    			lblTestBox.setBackground(new Color(shell.getDisplay(),col));
	    	}
	    });
	    this.btnBackground.setBounds(10, 21, 100, 45);
	    this.btnBackground.setImage(SWTResourceManager.getImage(EditorPreferences.class, "/com/google/code/cubeirc/resources/img_colors.png"));
	    this.btnBackground.setText("Background");
	    
	    this.btnForeground = new Button(this.shell, SWT.NONE);
	    this.btnForeground.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent arg0) {
	    		ColorDialog cl = new ColorDialog(shell);
	    		cl.setText("Select color for foreground");
	    		RGB col =  cl.open();
	    		if (col!= null)
	    			lblTestBox.setForeground(new Color(shell.getDisplay(),col));
	    	}	    	
	    });
	    this.btnForeground.setBounds(10, 72, 100, 45);
	    this.btnForeground.setImage(SWTResourceManager.getImage(EditorPreferences.class, "/com/google/code/cubeirc/resources/img_colors.png"));
	    this.btnForeground.setText("Foreground");
	    
	    this.btnFont = new Button(this.shell, SWT.NONE);
	    this.btnFont.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent arg0) {
	    		FontDialog fd = new FontDialog(shell);
	    		fd.setText("Select font for editor");
	    		
	    		FontData f = fd.open();
	    		if (f != null)
	    			lblTestBox.setFont(new Font(shell.getDisplay(), f));
	    	}
	    });
	    this.btnFont.setBounds(10, 123, 100, 45);
	    this.btnFont.setImage(SWTResourceManager.getImage(EditorPreferences.class, "/com/google/code/cubeirc/resources/img_fonts.png"));
	    this.btnFont.setText("Font");
	    
	    this.lblExample = new Label(this.shell, SWT.NONE);
	    this.lblExample.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
	    this.lblExample.setBounds(128, 21, 58, 15);
	    this.lblExample.setText("Example:");
	    
	    this.lblTestBox = new Label(this.shell, SWT.BORDER);
	    this.lblTestBox.setBounds(128, 42, 300, 126);
	    this.lblTestBox.setText("<user1> Hello!\r\n<user2> Hello!, How are You?\r\n<user1> fine thanks!, You?");
	    
	    this.btnClose = new Button(this.shell, SWT.NONE);
	    this.btnClose.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent arg0) {
	    	shell.dispose();	
	    	}
	    });
	    this.btnClose.setImage(SWTResourceManager.getImage(EditorPreferences.class, "/com/google/code/cubeirc/resources/img_exit.png"));
	    this.btnClose.setBounds(10, 174, 75, 25);
	    this.btnClose.setText("Close");
	    
	    this.btnSave = new Button(this.shell, SWT.NONE);
	    this.btnSave.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent arg0) {	    			    		
	    		EditorSettings ed = new EditorSettings(getEditorName(), lblTestBox.getFont().getFontData()[0].getName(), lblTestBox.getFont().getFontData()[0].getHeight() ,lblTestBox.getBackground().getRGB().toString(), lblTestBox.getForeground().getRGB().toString());
	    		Configuration.addEditorSettings(ed);
	    		new AlertBox(getParent(), ApplicationInfo.APP_FULL, "Editor preferences saved!", SWT.ICON_INFORMATION);
	    		shell.dispose();
	    	}
	    });
	    this.btnSave.setImage(SWTResourceManager.getImage(EditorPreferences.class, "/com/google/code/cubeirc/resources/img_save.png"));
	    this.btnSave.setBounds(353, 174, 75, 25);
	    this.btnSave.setText("Save");
	    
	    this.btnDefault = new Button(this.shell, SWT.NONE);
	    this.btnDefault.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent arg0) {
	    		EditorSettings ed;
	    		ed = Configuration.getEditorSettings(ApplicationInfo.EDT_DEFAULT_NAME);
	    		lblTestBox.setBackground(ed.getColorBackground());
	    		lblTestBox.setForeground(ed.getColorForeground());
	    		lblTestBox.setFont(new Font(shell.getDisplay(), ed.getFontname(), ed.getFontsize(), SWT.NONE));
	    	}
	    });
	    this.btnDefault.setImage(SWTResourceManager.getImage(EditorPreferences.class, "/com/google/code/cubeirc/resources/img_default.png"));
	    this.btnDefault.setBounds(272, 174, 75, 25);
	    this.btnDefault.setText("Default");
	    
	    loadConfiguration();
	    
	    
		while (!this.shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return this.result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void jbInit() {
		this.shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		this.shell.setSize(444, 242);
		this.shell.setText(getText());

	}
	
	private void loadConfiguration()
	{
		EditorSettings ed = Configuration.getEditorSettings(getEditorName());
		if (ed == null)
			ed = Configuration.getEditorSettings(ApplicationInfo.EDT_DEFAULT_NAME);
		
		lblTestBox.setBackground(ed.getColorBackground());
		lblTestBox.setForeground(ed.getColorForeground());
		lblTestBox.setFont(new Font(Display.getCurrent(), ed.getFontname(),ed.getFontsize(),SWT.NONE));
	}
}
